﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE.Tool.Forms;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE.Tool.UserControls
{
	public partial class ucJustNew : DevExpress.XtraEditors.XtraUserControl
	{
		private int index = 0;
		public int _action = 1;
		#region Handle Methods

		public ucJustNew()
		{
			InitializeComponent();
            BindDatatoGrid();
            gridView1.BestFitColumns();
		}

		private void btAddNew_Click_1(object sender, EventArgs e)
		{
			AddNew();
		}

		private void btEdit_Click_1(object sender, EventArgs e)
		{
			EditRow();
		}

		private void btDelete_Click_1(object sender, EventArgs e)
		{
			DeleteRows();
		}


		private void gridView1_CustomDrawEmptyForeground_1(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
		{
			if(gridView1.RowCount != 0)
				return;

			StringFormat drawFormat = new StringFormat();

			drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

			e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

		}

		private void gridView1_DoubleClick_1(object sender, EventArgs e)
		{
			var gridView = (GridView)sender;
			var point = gridView.GridControl.PointToClient(Control.MousePosition);

			DoDoubleClick(gridView, point);
		}


		private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AddNew();
		}

		private void editToolStripMenuItem_Click(object sender, EventArgs e)
		{
			EditRow();
		}

		private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			DeleteRows();
		}
		#endregion

		public void BindDatatoGrid()
		{
			JustNewHelper helper = new JustNewHelper(new JustNew());
			DataTable dt = helper.GetAll();

			gridControl1.DataSource = dt;
            Utilities.GridUtil.SelectRow(gridView1, index, _action);
		}

		private void DoDoubleClick(GridView v, Point p)
		{
			Cursor.Current = Cursors.WaitCursor;
			_action = 1;
			index = gridView1.FocusedRowHandle;
			var info = v.CalcHitInfo(p);
			if(info.InRow || info.InRowCell)
			{
				var row = new DataRow[gridView1.SelectedRowsCount];
				row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
				int iD = int.Parse(row[0][ToolConstants.Entities.JustNew.FieldName.ID].ToString());
				DateTime date = DateTime.Parse(row[0][ToolConstants.Entities.JustNew.FieldName.Date].ToString());
				string toolId = row[0][ToolConstants.Entities.JustNew.FieldName.ToolID].ToString();
				int quantity = int.Parse(row[0][ToolConstants.Entities.JustNew.FieldName.Quantity].ToString());
				int toolOrQTool = int.Parse(row[0][ToolConstants.Entities.JustNew.FieldName.ToolOrQTool].ToString());
				string note = row[0][ToolConstants.Entities.JustNew.FieldName.Note].ToString();

				frm_JustNewEdit fr = new frm_JustNewEdit(this);
				fr.Id = iD;
				fr.Date = date;
				fr.ToolId = toolId;
				fr.Quantity = quantity;
				fr.ToolOrQTool = toolOrQTool;
				fr.Note = note;

                fr.StartPosition = FormStartPosition.CenterParent;
				fr.ShowDialog();
			}
			Cursor.Current = Cursors.Default;
		}

		private void DeleteRows()
		{
			if(gridView1.SelectedRowsCount >0)
			{
				Cursor.Current = Cursors.WaitCursor;
				var row = new DataRow[gridView1.SelectedRowsCount];
				var JustNewId = new int[gridView1.SelectedRowsCount];

				for(int i=0; i< gridView1.SelectedRowsCount; i++)
				{
					row[i]= gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
					JustNewId[i] = int.Parse(row[i][ToolConstants.Entities.JustNew.FieldName.ID].ToString());
				}


				if(MessageBox.Show(ToolConstants.Message.DeleteQuestion, ToolConstants.Form.MessageBox.Warning,
								MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
				{
					JustNewHelper helper = new JustNewHelper(new JustNew());
					for(int i = 0; i < JustNewId.Length; i++)
					{
						helper.ValueObject.ID = JustNewId[i];
						if(helper.Delete() >0)
						{
							_action = 2;
							BindDatatoGrid();
						}
						else
						{
							MessageBox.Show(ToolConstants.Message.DeleteFailed, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}


				}
				Cursor.Current = Cursors.Default;
			}
			else
			{
				MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
								MessageBoxIcon.Information);
			}
		}

		private void EditRow()
		{
			if(gridView1.SelectedRowsCount >0)
			{
				Cursor.Current = Cursors.WaitCursor;
				frm_JustNewEdit fr = new frm_JustNewEdit(this);
				var row = new DataRow[gridView1.SelectedRowsCount];
				row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);

				index = gridView1.FocusedRowHandle;
				_action = 1;

				int iD = int.Parse(row[0][ToolConstants.Entities.JustNew.FieldName.ID].ToString());
				DateTime date = DateTime.Parse(row[0][ToolConstants.Entities.JustNew.FieldName.Date].ToString());
				string toolId = row[0][ToolConstants.Entities.JustNew.FieldName.ToolID].ToString();
				int quantity = int.Parse(row[0][ToolConstants.Entities.JustNew.FieldName.Quantity].ToString());
				int toolOrQTool = int.Parse(row[0][ToolConstants.Entities.JustNew.FieldName.ToolOrQTool].ToString());
				string note = row[0][ToolConstants.Entities.JustNew.FieldName.Note].ToString();

				fr.Id = iD;
				fr.Date = date;
				fr.ToolId = toolId;
				fr.Quantity = quantity;
				fr.ToolOrQTool = toolOrQTool;
				fr.Note = note;

                fr.StartPosition = FormStartPosition.CenterParent;
				fr.ShowDialog();
				Cursor.Current = Cursors.Default;

			}
			else
			{
				MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
								MessageBoxIcon.Information);
			}
		}

		private void AddNew()
		{
			Cursor.Current = Cursors.WaitCursor;
			Form fr = new frm_JustNewAdd(this);
            fr.StartPosition = FormStartPosition.CenterParent;
			fr.ShowDialog();
			Cursor.Current = Cursors.Default;
		}

	}
}
